﻿Shader "Hidden/Universal Render Pipeline/RadialBlurV2"
{
    Properties
    {
        _MainTex("Source", 2D) = "white" {}
    }

    HLSLINCLUDE

	#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"
	
 
	TEXTURE2D(_MainTex);			SAMPLER(sampler_MainTex);
	TEXTURE2D(_DitheringTexture);	
	TEXTURE2D(_RadialNoiseTex);		SAMPLER(sampler_RadialNoiseTex);

	

	uniform half4 _Params;
	uniform half _DarkThreshold;
	uniform half _ReverseParam;
	uniform float _DitheringTextureInvSize;
	uniform half4 _AdditionParams;

    #define _UseDither (mask)&1 == 1
    #define _BlackWithFlash (mask>>1)&1 == 1
    #define _UseNoiseTex  (mask>>2)&1 == 1
    #define _InverBlackWrith  (mask>>3)&1 == 1

	#define _NoiseRadius _AdditionParams.y
	#define _BlackWirthStep _AdditionParams.z
	#define _NoiseSpeed _AdditionParams.w
	
    #define _BlurRadius _Params.x
	#define _RadialCenter _Params.yz
	#define _BlurRange _Params.w

	#define UV (uv * scale + _RadialCenter)

	// Tranforms position from object to homogenous space
	inline float4 UnityObjectToClipPos(in float3 pos)
	{
	#if defined(STEREO_CUBEMAP_RENDER_ON)
		return UnityObjectToClipPosODS(pos);
	#else
		// More efficient than computing M*VP matrix product
		return mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(pos, 1.0)));
	#endif
	}
	inline float4 UnityObjectToClipPos(float4 pos) // overload for float4; avoids "implicit truncation" warning for existing shaders
	{
		return UnityObjectToClipPos(pos.xyz);
	}

	struct Attributes1
	{
		float4 positionOS       : POSITION;
		float2 texcoord         : TEXCOORD0;
		UNITY_VERTEX_INPUT_INSTANCE_ID
	};

	Varyings Vert1(Attributes1 input)
	{
		Varyings output;
		UNITY_SETUP_INSTANCE_ID(input);
		UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); 

		output.positionCS = UnityObjectToClipPos(input.positionOS);
    	output.texcoord = input.texcoord;
		
		return output;
	}

    

    float2 uv_to_polar(float2 uv)
    {
	    // 平移UV到中心
	    float2 centerUV = uv;

	    // 计算极坐标
	    float r = length(centerUV) * 2; // 乘以2保持默认范围在[0,1]
	    float theta = atan2(centerUV.y, centerUV.x);
	    // 将角度归一化到[0, 1]范围
	    theta = theta / (2 * PI) + 0.5;

	    return float2(theta, r);
    }

	half4 Frag_4Tap(Varyings i): SV_Target
	{
		half2 uv = i.texcoord - _RadialCenter;
		half d = 1;
		half d2 = 1;
		int mask = _AdditionParams.x;
		
		if(_UseDither)
		{
			half2 ditherUV = i.positionCS.xy * _DitheringTextureInvSize;
			d = SAMPLE_TEXTURE2D(_DitheringTexture, sampler_PointRepeat, ditherUV).a;
			d2 = 1-d; 
			d*=1.5;
			d2*=1.5;
			uv+=d2*half2(_ScreenParams.z-1,_ScreenParams.w-1)*0.01;
		}


		if (_UseNoiseTex)
		{
			half2 polar = uv_to_polar(uv);
			polar *= half2(30,0.5);
			polar.y= frac(polar.y-_Time.y*_NoiseSpeed);
			half ditherValue = SAMPLE_TEXTURE2D(_RadialNoiseTex, sampler_RadialNoiseTex, polar).r;
			ditherValue = saturate(ditherValue);
			half2 direction = (-uv);
			half v = distance(uv, half2(0, 0)) * 5;
			uv += direction * ditherValue * half2(_ScreenParams.z - 1, _ScreenParams.w - 1) * 100 * 1 *_NoiseRadius;
		}

		

		//减少两次采样作为代价 添加黑白爆的极坐标处理和Noise采样
		
		half scale = 1 + ( _BlurRadius*d2 ) * _ReverseParam ;
		half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		
		scale = 1 + ( 2 * _BlurRadius*d ) * _ReverseParam ;  //1 MAD
		half4 color2 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD

		color+=color2;
		
		scale = 1 + ( 3 * _BlurRadius*d2) * _ReverseParam ; //1 MAD
		half4 color3 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		color+=color3;
		
		color *= 0.333f; // 1/3
		if(_BlackWithFlash)
		{
			float i = _InverBlackWrith?1:0;
			half3 stepCololr = abs(i-step(_BlackWirthStep, Luminance( color)));
			color.rgb = lerp(stepCololr,color.rgb,_DarkThreshold);
		}

		color.a = 1;
		return  color;
	}
	
	half4 Frag_6Tap(Varyings i): SV_Target
	{
		half2 uv = i.texcoord - _RadialCenter;
		half scale = 1;
		half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( _BlurRadius ) * _ReverseParam;  //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( 2 * _BlurRadius ) * _ReverseParam; //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( 3 * _BlurRadius ) * _ReverseParam; //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( 4 * _BlurRadius ) * _ReverseParam; //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( 5 * _BlurRadius ) * _ReverseParam; //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		color *= 0.1667f; // 1/6
		return  color * _DarkThreshold;
	}
	
	half4 Frag_8Tap(Varyings i): SV_Target
	{
		half2 uv = i.texcoord - _RadialCenter;
		half scale = 1;
		half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( _BlurRadius ) * _ReverseParam;  //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( 2 * _BlurRadius ) * _ReverseParam; //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( 3 * _BlurRadius ) * _ReverseParam; //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( 4 * _BlurRadius ) * _ReverseParam; //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( 5 * _BlurRadius ) * _ReverseParam; //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( 6 * _BlurRadius ) * _ReverseParam; //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		scale = 1 + ( 7 * _BlurRadius ) * _ReverseParam; //1 MAD
		color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
		color *= 0.125f;  // 1/8
		return  color * _DarkThreshold;
	}
	
	// half4 Frag_10Tap(Varyings i): SV_Target
	// {
	// 	half2 uv = i.texcoord - _RadialCenter;
	// 	half scale = 1;
	// 	half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( _BlurRadius ) * _ReverseParam;  //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 2 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 3 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 4 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 5 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 6 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 7 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 8 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 9 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	color *= 0.1f;  // 1/10
	// 	return  color * _DarkThreshold;
	// }
	
	// half4 Frag_12Tap(Varyings i): SV_Target
	// {
	// 	half2 uv = i.texcoord - _RadialCenter;
	// 	half scale = 1;
	// 	half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( _BlurRadius ) * _ReverseParam;  //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 2 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 3 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 4 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 5 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 6 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 7 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 8 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 9 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 10 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 11 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	color *= 0.0833f;  // 1/12
	// 	return  color * _DarkThreshold;
	// }
	
	// half4 Frag_20Tap(Varyings i): SV_Target
	// {
	// 	half2 uv = i.texcoord - _RadialCenter;
	// 	half scale = 1;
	// 	half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( _BlurRadius ) * _ReverseParam;  //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 2 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 3 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 4 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 5 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 6 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 7 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 8 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 9 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 10 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 11 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 12 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 13 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 14 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 15 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 16 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 17 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 18 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	scale = 1 + ( 19 * _BlurRadius ) * _ReverseParam; //1 MAD
	// 	color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UV); //1 MAD
	// 	color *= 0.05f;  // 1/20
	// 	return color * _DarkThreshold;
	// }

    ENDHLSL
 
    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
		ZTest Always ZWrite Off Cull Off

		Pass
        {
            HLSLPROGRAM			
            #pragma vertex Vert1
            #pragma fragment Frag_4Tap
            ENDHLSL
        }

		Pass
        {
            HLSLPROGRAM			
            #pragma vertex Vert1
            #pragma fragment Frag_6Tap
            ENDHLSL
        }

		Pass
        {
            HLSLPROGRAM			
            #pragma vertex Vert1
            #pragma fragment Frag_8Tap
            ENDHLSL
        }

		// Pass
        // {
        //     HLSLPROGRAM			
        //     #pragma vertex Vert
        //     #pragma fragment Frag_10Tap
        //     ENDHLSL
        // }

		// Pass
        // {
        //     HLSLPROGRAM			
        //     #pragma vertex Vert
        //     #pragma fragment Frag_12Tap
        //     ENDHLSL
        // }

		// Pass
        // {
        //     HLSLPROGRAM			
        //     #pragma vertex Vert
        //     #pragma fragment Frag_20Tap
        //     ENDHLSL
        // }
    }
}